<template>
  <div id="home">
    <nav-bar class="home-nav">
      <template #center>
        购物街
      </template>
    </nav-bar>

    <tab-control
      class="fixed"
      v-show="isTabShow"
      ref="tabControl2"
      @tabClick="tabClick"
      :titles="['流行', '新款', '精选']"></tab-control>

    <scroll class="content" ref="scroll"
            @scroll="contentScroll" @getMore="loadMore"
            :probe="3" :pull-up="true">
      <template #default>
        <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"></home-swiper>

        <home-recommend :recommends="recommends"></home-recommend>

        <home-feature></home-feature>

        <tab-control
          ref="tabControl"
          @tabClick="tabClick"
          :titles="['流行', '新款', '精选']"></tab-control>

        <goods-list
          :goods="goods[tabType].list"
          :tabType="tabType"
        ></goods-list>
      </template>
    </scroll>

    <back-top v-show="isShow" @backClick="backClick" right="10" bottom="50"></back-top>

  </div>
</template>

<script>
  import { debounce } from 'common/utils'
  import { getHomeMultidata, getHomeGoods } from 'network/home'
  import NavBar from 'components/common/navbar/NavBar'
  import TabControl from 'components/content/tabControl/TabControl'
  import GoodsList from 'components/content/goods/GoodsList'
  import Scroll from 'components/common/scroll/Scroll'
  import BackTop from 'components/content/backTop/BackTop'

  import HomeSwiper from './childComps/HomeSwiper'
  import HomeRecommend from './childComps/HomeRecommend'
  import HomeFeature from './childComps/HomeFeature'

  export default {
    name: "Home",
    components:{
      NavBar,
      HomeSwiper,
      HomeRecommend,
      HomeFeature,
      TabControl,
      GoodsList,
      Scroll,
      BackTop
    },
    data(){
      return {
        banners: null,
        recommends: null,
        goods: {
          'pop': { page: 0, list: [] },
          'news': { page: 0, list: [] },
          'sell': { page: 0, list: [] }
        },
        tabType: 'pop',
        isShow: false,
        tabOffsetTop: 0,
        isTabShow: false,
        saveY: 0,
        itemImgListener: null
      }
    },
    activated(){
      // 设置之前最好刷新一下
      this.$refs.scroll.refresh()

      // 第一个.scroll是获取组件，第二个是获取data中的scroll
      this.$refs.scroll.scroll.scrollTo(0, this.saveY, 0)
      // this.$refs.scroll.scroll.startY = this.saveY
    },
    deactivated(){
      this.saveY = this.$refs.scroll.scroll.y

      // 取消全局事件的监听
      this.$bus.$off('itemImageLoad', this.itemImgListener)
    },
    created(){
      // 请求多个数据
      this.getHomeMultidata()
      // 请求商品数据
      this.getHomeGoods('pop')
      this.getHomeGoods('news')
      this.getHomeGoods('sell')
    },
    mounted(){
      const refresh = debounce(this.$refs.scroll && this.$refs.scroll.refresh, 200)

      this.itemImgListener = () => {
        refresh()
      }
      // 监听item中图片加载完成
      this.$bus.$on('itemImageLoad', this.itemImgListener)
    },
    methods:{
      /*
      * 事件监听
      * */
      swiperImageLoad(){
        this.tabOffsetTop = this.$refs.tabControl.$el.offsetTop
        console.log(this.$refs.tabControl.$el.offsetTop);
      },
      tabClick(index){
        switch (index) {
          case 0:
            this.tabType = 'pop'
            break
          case 1:
            this.tabType = 'news'
            break
          case 2:
            this.tabType = 'sell'
            break
        }

        // 将两个状态选中对应
        this.$refs.tabControl2.currentIndex = index
        this.$refs.tabControl.currentIndex = index
      },
      backClick(){
        // 直接通过 ref 获取组件，调用组件内部方法
        this.$refs.scroll.backTop(0, 0, 300)
      },
      contentScroll(position){
        // console.log(position);

        // 判断BackTop是否显示
        this.isShow = position.y < -600

        // 判断tabControl是否吸顶
        this.isTabShow = -position.y > this.tabOffsetTop
      },
      loadMore(){ //加载更多数据
        this.getHomeGoods(this.tabType)
      },
      /*
      * 网络请求
      * */
      getHomeMultidata(){
        getHomeMultidata().then(res => {
          this.banners = res.data.data.banner.list
          this.recommends = res.data.data.recommend.list
        })
      },
      getHomeGoods(type){
        let page = this.goods[type].page + 1
        getHomeGoods().then(res => {
          var ret = res.data.message[type].find(t => t.page===page)
          if(!ret){
            this.$refs.scroll.finishPullUp()
            return console.log('没有更多数据了...')
          }
          // 为防止数据被覆盖，则要将之前数组数据合并新的一页
          this.goods[type].list = this.goods[type].list.concat(ret.list)

          // console.log(page);
          // 获取完后让page页数 +1
          this.goods[type].page = page

          this.$nextTick(() => {
            // 能再次上拉加载
            this.$refs.scroll.finishPullUp()
            // 刷新高度
            this.$refs.scroll.refresh()
          })

        })
      }
    }
  }
</script>

<style lang="less" scoped>
#home{
  position: relative;
  height: 100vh;
  /*padding: 44px 0 0;*/
  .home-nav{
    background-color: var(--color-tint);
    color: white;
    font-weight: bold;

    /*position: fixed;*/
    /*top: 0;*/
    /*left: 0;*/
    /*right: 0;*/
    /*z-index: 9;*/
  }
  .content{
    /*height: calc(100vh - 93px);*/
    overflow: hidden;

    position: absolute;
    top: 44px;
    bottom: 49px;
    left: 0;
    right: 0;
  }
  .fixed{
    position: relative;
    z-index: 99;
    background-color: #fff;
  }
}
</style>
